<template>
  <el-container>
    <el-header>
      <div class="logo">
        <img src="../assets/img/logo.png" alt="" srcset="" />
        <span>教务管理系统</span>
      </div>
      <div class="user">
        <el-switch
          inactive-icon-class="el-icon-sunny"
          active-icon-class="el-icon-moon"
          v-model="isDark"
        >
        </el-switch>
        <img src="../assets/img/user.png" alt="" srcset="" />
        <el-dropdown>
          <span class="el-dropdown-link">
            <!-- 从中央仓库中取值 -->
            <span>{{ user.username }}</span>
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <span class="el-icon-setting">个人设置</span>
              </el-dropdown-item>
              <el-dropdown-item>
                <span class="el-icon-switch-button" @click="loginOut()">退出登录</span>
              </el-dropdown-item>
              <!-- 子组件标签上绑定的任何事件，都是父向子传递的自定义事件，不看事件名 -->
              <!-- 如果需要将事件作为原生元素事件使用，可以用过 .native 监听组件根元素的原生事件。 -->
              <el-dropdown-item @click.native="loginOut()">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <el-aside width="220px">
        <el-menu :router="true" :default-active="$route.path">
          <el-menu-item v-for="menu in menus" :key="menu.right_id" :index="menu.right_path">
            <span class="fs" v-html="menu.right_icon"></span>
            <span>{{ menu.right_name }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view class="main-content"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// import { getMenu } from "@/api";
export default {
  // data() {
  //   return {
  //     menus: [],
  //   };
  // },
  computed: {
    menus() {
      return this.$store.state.menus;
    },
    isDark: {
      get() {
        return this.$store.state.isDark;
      },
      set(nv) {
        this.$store.commit("setIsDark", nv);
      },
    },
    user() {
      return this.$store.state.user;
    },
  },
  methods: {
    changeTheme() {
      this.isDark = !this.isDark;
    },
    // async initMenu() {
    //   let { data } = await getMenu();
    //   this.menus = data;
    //   this.$store.commit("setMenus", data);
    // },
    loginOut() {
      // 清空登录状态
      this.$store.dispatch("loginOut");
      // 跳转登录页面
      this.$router.push({
        name: "login",
      });
    },
  },
  // created() {
  //   this.initMenu();
  // },
};
</script>

<style lang="scss" scoped>
// 调用主题颜色
.el-container {
  height: 100%;
}
.el-header {
  // 头部导航的背景颜色和阴影
  box-shadow: 0px 0 6px var(--shadow-color);
  background-color: var(--bg-color);
  z-index: 2;
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  align-items: center;
  .logo {
    display: flex;
    flex-flow: row nowrap;
    justify-content: flex-start;
    align-items: center;
    height: 60px;
    line-height: 60px;
    font-size: 20px;
    font-weight: bold;
    color: var(--font-color);
    img {
      width: 40px;
      height: 40px;
      margin-right: 8px;
    }
  }
  .el-switch {
    margin-right: 20px;
  }
  .user {
    display: flex;
    flex-flow: row nowrap;
    justify-content: flex-end;
    align-items: center;
    height: 60px;
    line-height: 60px;
    font-size: 20px;
    font-weight: bold;
    color: var(--font-color);
    img {
      width: 40px;
      height: 40px;
      margin-right: 8px;
      border-radius: 50%;
      background-color: #42a1ff66;
    }
  }
  .el-dropdown {
    cursor: pointer;
  }
}
.el-aside {
  box-shadow: 0 2px 6px var(--shadow-color);
  background-color: var(--bg-color);
}
.el-menu {
  border-right: none;
  .el-menu-item span {
    vertical-align: middle;
  }
  .fs {
    font-size: 0;
  }
  ::v-deep svg {
    width: 18px;
    height: 18px;
    margin-right: 4px;
  }
  ::v-deep svg > * {
    stroke: var(--font-color);
  }
  .is-active ::v-deep svg > * {
    stroke: #409eff;
  }
}

// 为子组件提供样式
.main-content ::v-deep {
  height: 100%;
  box-sizing: border-box;
  // 通过设置弹性盒子，借助flex-grow填充内容空间
  display: flex;
  flex-flow: column nowrap;
  .el-card__body {
    flex-grow: 1;
    // 通过层级关键，一步一步进行高度填充传递
    display: flex;
    flex-flow: column nowrap;
  }
  .el-table {
    // 为表格填充剩余空间，在限制的页面视觉范围内展示内内容
    // 必须注意 elementUI 的 el-table 只有在定义了 height 时才会出现高度滚动条
    // 弹性盒子的 flex-grow 用于高于样式的 height
    flex-grow: 1;
  }
  .btns {
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    margin-bottom: 20px;
    .el-input {
      width: 160px;
      margin-right: 10px;
    }
  }
  .el-tag {
    display: block;
    text-align: center;
  }
  .el-pagination {
    margin-top: 20px;
    text-align: center;
  }
}
</style>
